/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.service;

import java.util.Date;
import java.util.Set;

import com.javaeye.delivery.dto.CustomerOrder;
import com.javaeye.delivery.dto.OrderDetail;
import com.javaeye.order.dto.PurchaseOrder;
import com.javaeye.order.dto.PurchaseOrderDetail;
import com.javaeye.delivery.dto.Reason;
import com.javaeye.common.util.DateUtils;

public class OrderPlanService extends OrderService {
	
	
	public void saveOrder(CustomerOrder order) {
		double hours = DateUtils.getDateOffsetHour(order.getUpdateDate(), DateUtils.now());
		if (hours > Reason.LATER_HOUR) {
			Reason reason = new Reason(Reason.REASON_CATEGORY_PLAN, 
					order.getId(),
					"计划安排在业务创建出货单完成" + Reason.LATER_HOUR + "个小时之后");
			getReasonDao().saveReason(reason);
		}
		
		//如果计划完成时间晚于预出货日期，记录一个异常点
		Date expectOutDate = order.getExpectOutDate();
		Set<OrderDetail> details = order.getOrderDetails();
		for (OrderDetail detail : details) {
			if (detail.getFinishDate().after(expectOutDate)) {
				Reason reason = new Reason(Reason.REASON_CATEGORY_PLAN, 
						order.getId(),
						"计划安排完成时间 ("+ DateUtils.formatString(detail.getFinishDate(), "yyyy-MM-dd") 
						+ ") 晚于预出货日期(" + DateUtils.formatString(expectOutDate, "yyyy-MM-dd") + ")");
				getReasonDao().saveReason(reason);
				break;
			}
		}
		
		//更新出货单为已经计划状态
		order.setStatus(ORDER_STATES_PLAN);
		order.setUpdateDate(DateUtils.now());
		getDao().saveOrder(order);

		String purchaseOrderId = order.getPurchaseOrderId();
		int productId;
		PurchaseOrderDetail purchaseOrderdetail;
		//Set<OrderDetail> details = order.getOrderDetails();
		for (OrderDetail detail : details) {
			productId = detail.getProductId();
			//更新订单表里的已经计划数量
			purchaseOrderdetail = getPurchaseOrderDetailDao()
				.getPurchaseOrderDetail(purchaseOrderId, productId);
			purchaseOrderdetail.setPlanNumber(purchaseOrderdetail.getPlanNumber() + detail.getNumber());
			getPurchaseOrderDetailDao().savePurchaseOrderDetail(purchaseOrderdetail);
		}
		
		//如果全部计划完成更新订单表的状态
		PurchaseOrder purchaseOrder = getPurchaseOrderDao().getPurchaseOrderInfo(purchaseOrderId);
		Set<PurchaseOrderDetail> alldetails = purchaseOrder.getOrderDetails();
		if (isPlanFinished(alldetails)) {
			purchaseOrder.setStatus(IOrderService.ORDER_STATES_PLAN);
			getPurchaseOrderDao().savePurchaseOrder(purchaseOrder);
		}
	}
	
	/**
	 * 是否全部计划完成
	 * @param details
	 * @return
	 */
	private boolean isPlanFinished(Set<PurchaseOrderDetail> details) {
		int allCount = details.size();
		int finishCount = 0;
		for (PurchaseOrderDetail detail : details) {
			if (detail.getPlanNumber() == detail.getNumber()) {
				finishCount++;
			}
		}
		return finishCount == allCount;
	}

}
